﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenExpressApp.Template;
using OpenExpressApp.MetaModel;
using System.Windows.Controls;
using System.Windows;
using Itenso.Windows.Input;
using Csla.Core;
using OpenExpressApp.Types;
using AvalonDock;
using OpenExpressApp.WPF.Command;

namespace OpenExpressApp.Module.WPF.Templete
{
    public class PopupFormController : BaseListFrameController
    {
        private Panel _conditionQueryRegion;

        private Panel _navigateQueryRegion;

        private Panel _listRegion;

        private ListViewController _listViewController;

        public PopupFormController(IWindowTemplate windowTemplate)
            : base(windowTemplate)
        {
            this._listViewController = new ListViewController(windowTemplate.View as ListObjectView);

            this._listRegion = windowTemplate.GetRegion(ViewRegionNames.ListRegion) as Panel;

            this._listRegion.Children.Add(this._listViewController.Control);

            this.GenerateQueryPanel();

            this.Layout();

            ExecAutoCommands(this._listViewController.View.EntityType, this._listViewController.View);
        }

        private void GenerateQueryPanel()
        {
            var view = this._listViewController.View;
            //生成条件查询面板
            var conView = view.CondtionQueryView;
            if (conView != null)
            {
                this._conditionQueryRegion = this.FrameTemplate.GetRegion(ViewRegionNames.ConditionQueryRegion) as Panel;

                StackPanel pnlQueryButton = new StackPanel() { Orientation = Orientation.Horizontal };

                Button btnQuery = new Button();
                btnQuery.CommandParameter = conView;
                ButtonCommand.SetCommand(btnQuery, CommandRepository.NewCommand(CommandNames.QueryObject));
                pnlQueryButton.Children.Add(btnQuery);

                Button btnClearCondition = new Button();
                btnClearCondition.CommandParameter = conView;
                ButtonCommand.SetCommand(btnClearCondition, CommandRepository.NewCommand(CommandNames.ClearQueryCondition));
                pnlQueryButton.Children.Add(btnClearCondition);

                pnlQueryButton.SetValue(DockPanel.DockProperty, Dock.Top);
                this._conditionQueryRegion.Children.Add(pnlQueryButton);
                ScrollViewer sv = new ScrollViewer();
                FrameworkElement conditionQueryControl = conView.Control as FrameworkElement;
                sv.Content = conditionQueryControl;
                this._conditionQueryRegion.Children.Add(sv);

                //初始化对象
                conView.Data = Activator.CreateInstance(conView.EntityType);
                //(conView.CurrentObject as BusinessBase).BeginEdit();
            }

            //生成导航查询面板
            var naviView = view.NavigateQueryView;
            if (naviView != null)
            {
                this._navigateQueryRegion = this.FrameTemplate.GetRegion(ViewRegionNames.NavigateQueryRegion) as Panel;
                this._navigateQueryRegion.Children.Add(naviView.Control as FrameworkElement);

                //初始化对象
                naviView.Data = Activator.CreateInstance(naviView.EntityType);
               // (naviView.CurrentObject as BusinessBase).BeginEdit();
            }
        }

        private void Layout()
        {
            //如果导航查询面板放在列表上面
            var info = ApplicationModel.GetBusinessObjectInfo(this.View.EntityType);
            if (info.NavigateQueryRegionPoistion == QueryRegionPosition.ResultView)
            {
                var element = (this._navigateQueryRegion.Parent as FrameworkElement).Parent as FrameworkElement;
                element.Visibility = Visibility.Collapsed;
                (this._navigateQueryRegion.Parent as DockableContent).Content = null;
                this._navigateQueryRegion.SetValue(DockPanel.DockProperty, Dock.Top);
                this._listRegion.Children.Insert(0, this._navigateQueryRegion);
            }
        }
    }
}
